package sync;

import bluecove.BlueSync;
import com.google.gdata.client.calendar.CalendarQuery;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import config.ConsoleUtil;

import java.net.URL;
import java.util.Calendar;
import java.util.Date;

import gdata.GUtil;

public class CalendarTest {

  public static void main(String[] args) throws Exception {
    String USERNAME = "gantz13@gmail.com";
    URL feedUrl = new URL("http://www.google.com/calendar/feeds/" + USERNAME + "/private/full");

    CalendarQuery myQuery = new CalendarQuery(feedUrl);
    myQuery.setMinimumStartTime(DateTime.now());
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, 15);
    myQuery.setMaximumStartTime(new DateTime(calendar.getTime()));

    CalendarService myService = GUtil.getCalendarService();

    CalendarEventFeed myResultsFeed = myService.query(myQuery, CalendarEventFeed.class);

    StringBuffer events = new StringBuffer();
    events.append("<html>");
    int i = 0;
    for (CalendarEventEntry entry : myResultsFeed.getEntries()) {
      DateTime startTime = entry.getTimes().get(0).getStartTime();
      events.append("<p style=\"color:" + ConsoleUtil.getNextColor(i++) + ";\">");
      events.append("<b>");
      events.append(new Date(startTime.getValue()));
      events.append("<br/>");
      String title = entry.getTitle().getPlainText();
      events.append(title);
      events.append("<b>");
      events.append("</p>");
      events.append("\n");
    }
    events.append("</ul></html>");
    ConsoleUtil.debug(events.toString());
    BlueSync.send(BlueSync.DEFAULT_PATH, "Calendar.html", events.toString());

  }
}
